数组函数some、every、find、filter、map、forEach有什么区别 您所在的位置:网站首页 each year和every year有什么区别 数组函数some、every、find、filter、map、forEach有什么区别

数组函数some、every、find、filter、map、forEach有什么区别

2024-07-02 07:39| 来源: 网络整理| 查看: 265

在这里插入图片描述

some

1、不创建新数组 2、不改变原数组 3、输出的是判断为true则马上跳出循环并return成true 4、回调函数参数,item(数组元素)、index(序列)、arr(数组本身) 5、使用return操作输出,会循环数组每一项,并在回调函数中操作

//计算对象数组中每个电脑的操作系统是否可用, //大于16位操作系统表示可用,否则不可用 var computers = [ { name: "Apple", ram: 8 }, { name: "IBM", ram: 4 }, { name: "Acer", ram: 32 }, ]; var some = computers.some(function (computer) { return computer.ram > 16; }); console.log(some);//true console.log(computers);//[{ name: "Apple", ram: 8 },{ name: "IBM", ram: 4 },{ name: "Acer", ram: 32 }] every(与some相反)

1、不创建新数组 2、不改变原数组 3、输出的是判断为false则马上跳出循环并return成false 4、回调函数参数,item(数组元素)、index(序列)、arr(数组本身) 5、使用return操作输出,会循环数组每一项,并在回调函数中操作

var computers = [ { name: "Apple", ram: 8 }, { name: "IBM", ram: 4 }, { name: "Acer", ram: 32 }, ]; var every = computers.every(function (computer) { return computer.ram > 16; }); console.log(every);//false find

1、不创建新数组 2、不改变原数组 3、输出的是一旦判断为true则跳出循环输出符合条件的数组元素 4、回调函数参数,item(数组元素)、index(序列)、arr(数组本身) 5、使用return操作输出,会循环数组每一项,并在回调函数中操作

//假定有一个对象数组,找到符合条件的对象 var users = [ { name: 'Jill' }, { name: 'Alex', id: 1 }, { name: 'Bill' }, { name: 'Alex' }, ]; var user = users.find(function (user) { return user.name === 'Alex'; }); console.log(user);//[{ name: 'Alex', id: 1 }] //假定有一个对象数组(A),根据指定对象的条件找到数组中符合条件的对象 var posts = [ { id: 1, title: "Node.js" }, { id: 2, title: "React.js" }, ]; var comment = { postId: 1, content: 'hello' }; function postForComment(posts, comment) { return posts.find(function (post) { return post.id === comment.postId }) }; console.log(postForComment(posts,comment));//{ id: 1, title: "Node.js" } filter

1、创建新数组 2、不改变原数组 3、输出的是判断为true的数组元素形成的新数组 4、回调函数参数,item(数组元素)、index(序列)、arr(数组本身) 5、使用return操作输出,会循环数组每一项,并在回调函数中操作

//假定有一个对象数组(A),获取数组中指定类型的对象放到B数组中 var products = [ { name: "cucumber", type: "vegetable" }, { name: "banana", type: "fruit" }, { name: "celery", type: "vegetable" }, { name: "orange", type: "fruit" }, ]; var filtered = products.filter(function (product) { return product.type === "vegetable" }); console.log(filtered);//[{ name: "cucumber", type: "vegetable" }, { name: "celery", type: "vegetable" }] //假定有一个对象数组(A),过滤掉不满足以下条件的对象 //条件:蔬菜 数量大于0 价格小于10 var products = [ { name: "cucumber", type: "vegetable", quantity: 0, price: 1 }, { name: "banana", type: "fruit", quantity: 10, price: 16 }, { name: "celery", type: "vegetable", quantity: 30, price: 8 }, { name: "orange", type: "fruit", quantity: 3, price: 6 }, ]; products = products.filter(function (product) { return product.type === 'vegetable' && product.quantity > 0 && product.price { num=num*2; if(num>50){ finalList.push(num); } return finalList; },[]); console.log(doubleedOver50);//[60,80] //将数组中对象的某个属性抽离到另外一个数组中 var primaryColors = [ { color: 'red' }, { color: 'yellow' }, { color: 'blue' }, ]; var color = primaryColors.reduce(function (previous, primaryColor) { previous.push(primaryColor.color); return previous; }, []); console.log(color);//["red", "yellow", "blue"] // 判断字符串括号是否对称,遇到(时加1,遇到)时减1 function balancedParens(string) { return !string.split("").reduce(function (previous, char) { if (previous


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有